/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef UGAME_NET_TCPSOCKET_H_
#define UGAME_NET_TCPSOCKET_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cstddef>
#include <cstdint>
#include <string>
#include <memory>
#include <UGame/Macros.h>

struct sockaddr;
struct event_base;
struct bufferevent;

namespace UGame {
namespace Net {
    class TcpConnection;
    class TcpRunnable;
    class TcpServer;

    class TcpSocket
    {
    public:
        enum EventFlags
        {
            EF_READ = 0x1,
            EF_WRITE = 0x2
        };
        enum ShutdownHow
        {
            SH_READ = 0x1,
            SH_WRITE = 0x2
        };

        TcpSocket(void);
        ~TcpSocket(void);

        bool Open(struct event_base *_Base, int _Sock, struct sockaddr *_SockAddr = NULL, int _AddrLen = 0);
        void Close(void);

        int id(void) const { return _id; }
        const std::string& host(void) const { return _host; }
        int port(void) const { return _port; }

        const std::string& GetErrorString(void) const { return _errorString; }
        int GetErrorCode(void) const { return _errorCode; }

        void Send(const std::uint8_t *_Data, std::size_t _Size);

        std::size_t GetReceivedSize(void);
        int Read(std::uint8_t *_Data, std::size_t _Size);
        int ReadPeek(std::uint8_t *_Data, std::size_t _Size);
        std::size_t ReadSkip(std::size_t _Size);

        void EnableReceive(bool _Enable);
        void EnableSend(bool _Enable);

        void Shutdown(int _How);

    private:
        friend class TcpServer;

        void _SetId(int _Id) { _id = _Id; }
        void _SetOwner(TcpServer *_Owner) { _owner = _Owner; }
        void _SetThread(TcpRunnable *_Runnable) { _runnable = _Runnable; }
        void _SetEventHandler(TcpConnection *_Handler) { _handler.reset(_Handler); }
        void _Unintialize(void);

        void _OnError(void);

        static void _ReadCallback(struct bufferevent *, void *);
        static void _WriteCallback(struct bufferevent *, void *);
        static void _EventCallback(struct bufferevent *, short, void *);

        int _id;
        int _sock;
        struct bufferevent *_bev;
        std::auto_ptr<TcpConnection> _handler;
        std::string _host;
        int _port;
        std::string _errorString;
        int _errorCode;
        volatile bool _fatalError;
        TcpServer *_owner;
        TcpRunnable *_runnable;
        DISALLOW_COPY_AND_ASSIGN(TcpSocket);
    };

}}

#endif // UGAME_NET_TCPSOCKET_H_
